//
//  UIBarButtonItem+Extension.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/12.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
 
    //通过构造方法来扩展方法
    
    convenience  init(imageName: String, action:String?,target:AnyObject) {
       let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
         button.setImage(UIImage(named:"\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        //添加点击方法
        if let action = action {
          //注意按钮的点击方法 不是字符串 是Selector
        button.addTarget(target, action: Selector(action), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        button.sizeToFit() //自适应
      self.init(customView:button)
    }

    

}
